home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-11-30 | 9.0 KB | 270 lines | [TEXT/CWIE] |
- // Apple Event Functions
- #ifndef __FXNPROTOS__
- #include "FxnProtos.h"
- #endif
-
- #include "CDEF Demo App.h"
- #include "FxnProtos.h"
-
- extern Boolean gDone;
-
-
- /***************************************************************************************************
- AEHasRequiredParameters
-
- Checks to see if all the parameters were extracted from the AppleEvent. The keyMissedKeywordAttr
- contains the first required parameter that your handlers didn't retrieve, if any. If doesn't
- contain the keyMissedKeywordAttr attribute you extracted all the parameters and you can continue
- to process the AppleEvent. If you not, you should stop processing the AppleEvent.
- ***************************************************************************************************/
-
- OSErr AEHasRequiredParameters( AppleEvent *ae )
- {
- OSErr error;
- DescType returnedType;
- Size actualSize;
-
- // Look for the missing keyword attribute. If we got it, there is something
- // missing.
-
- error = AEGetAttributePtr( ae, keyMissedKeywordAttr, typeWildCard, &returnedType,
- NULL, 0, &actualSize );
- if( error == errAEDescNotFound )
- {
- // You got all the required parameters
-
- return( noErr );
- }
- else
- {
- // You didn't get all the parameters or there was some other error causing
- // a problem. Either way, you need to stop processing the AppleEvent if this
- // function returns anything but, noErr.
-
- if( error != noErr ) return( error );
- return( errAEParamMissed );
- }
- }
-
-
- /*************************************************************************************
- AEOpenApplication
-
- AppleEvent handler for the OpenApp event. This will be called when the application
- is launched without any documents to be opened or printed. We just return the
- error (if any) if we didn't get all the required parameters. You could also have
- code here to put up a splash screen but, remember that this can be called more
- than just when your application is first launched. The Finder is smart enough
- not to send you another OpenApp event but, other people launching your app thru
- some other means may not be.
- *************************************************************************************/
-
- pascal OSErr AEOpenApplication( AppleEvent *ae, AppleEvent *reply, long refCon )
- {
- OSErr error;
-
- // Check for any missing required parameters
-
- error = AEHasRequiredParameters( ae );
- if( error != noErr ) return( error );
-
- // If we wanted to do anything when the application is launched without any
- // documents to be opened or printed, we would do it here. Display a splash
- // screen, open a new window, etc...
-
- // Set the gAutoQuit flag to false since we never want to auto quit when we get
- // an OpenApplication event (ie. the user double-clicked on us).
-
- // gAutoQuit = false;
-
- return( error );
- }
-
- /*************************************************************************************
- AEOpenDocument
-
- AppleEvent handler for the OpenDoc event. When the user drops files onto your
- application or they double click on documents your application created, you'll get
- this event. We just pass the event onto our generic AEHandleDocuments() with a
- flag set for opening files.
- *************************************************************************************/
-
- pascal OSErr AEOpenDocuments( AppleEvent *ae, AppleEvent *reply, long refCon )
- {
- // This just sets the kOpenDocument flag so we know what type of event it is and
- // passed all the parameters onto the generic AEHandleDocuments routine.
-
- return( AEHandleDocuments( ae, reply, refCon ) );
- }
-
- /*************************************************************************************
- AEPrintDocument
-
- AppleEvent handler for the PrintDoc event. When the user prints files from the
- Finder or some other utility that prints from your application, you'll get this
- event. We just pass the event onto our generic AEHandleDocuments() routine with
- a flag for printing files.
- *************************************************************************************/
-
- pascal OSErr AEPrintDocuments( AppleEvent *ae, AppleEvent *reply, long refCon )
- {
- // Handle PrintDoc events similar to how OpenDoc events are handle by stepping
- // thru each document and printing it. Make sure to stay open cause the Finder
- // will send you a Quit event if you need to quit when done printing.
-
- // gAutoQuit = false;
-
- // If we can interact with the user, we should open all the documents
- // and display the print dialog box to let the user choose the correct
- // settings and print all the documents from with those settings. If
- // we can't interact with the user, we can either print the documents
- // with the default settings or return the errAENoUserInteraction error
-
- return( errAEEventNotHandled );
- }
-
- /*************************************************************************************
- AEQuitApplication
-
- AppleEvent handler for the QuitApp event. You'll get this when the user chooses
- quit or when the Finder sends you this event to quit the app for a restart, or
- some other reason. This should be the only way for the app to quit. We just set a
- flag indicating we want to quit which force our MainEventLoop to fall thru and
- quit the application.
- *************************************************************************************/
-
- pascal OSErr AEQuitApplication( AppleEvent *ae, AppleEvent *reply, long refCon )
- {
- OSErr error;
- DescType saveOptions, returnedType;
- Size actualSize;
-
- // Check to see if there's a save option parameter in the AppleEvent.
-
- error = AEGetParamPtr( ae, keyAESaveOptions, typeEnumerated, &returnedType,
- &saveOptions, sizeof( saveOptions ), &actualSize );
-
- // If we couldn't find a keyAESaveOptions parameter in this AppleEvent, we
- // assume default (kAEAsk) and set the error to noErr.
-
- if( error == errAEDescNotFound ) { error = noErr; saveOptions = kAEAsk; }
- if( error != noErr ) return( error );
-
- // Check for any missing required parameters. There is an error in the sample
- // code in Inside Mac: Interapplication Communication. It says you should call
- // your HasRequiredParameters routine first which you shouldn't since it will
- // incorrectly return that you didn't get all the parameters (since there is
- // possibly the save options parameter which is optional (ie. save "options").
-
- error = AEHasRequiredParameters( ae );
- if( error != noErr ) return( error );
-
- // Set the quit flag so our MainEventLoop will know we want to quit.
-
- // gQuit = true;
-
- gDone = true;
-
- return( error );
- }
-
- /*************************************************************************************
- AEHandleDocuments
-
- This is our generic AppleEvent handler for dealing with document events. It takes
- the AppleEvent and based on the event type, handles processing the documents
- the correct way.
- *************************************************************************************/
-
- pascal OSErr AEHandleDocuments( AppleEvent *ae, AppleEvent *reply, SInt32 refCon )
- {
- OSErr error;
- AEDescList list;
- SInt32 count, i;
- FSSpec fs;
- EventRecord event;
-
- // Get the direct parameter, a list of descriptors (ie. files in this case), and put it into the
- // list.
-
- error = AEGetParamDesc( ae, keyDirectObject, typeAEList, &list );
- if( error != noErr ) return( error );
-
- // Check for any missing parameters.
-
- error = AEHasRequiredParameters( ae );
- if( error != noErr ) goto error;
-
- // Count the number of items in the document list (There may be folders which only count as one,
- // here (they'll be handled correctly later).
-
- error = AECountItems( &list, &count );
- if( error != noErr ) goto error;
-
- for( i = 1; i <= 1; i++ )
- {
- // Coerce the item in the AppleEvent descriptor list into an FSSpec.
-
- error = AEGetNthFSSpec( &list, i, &fs );
- if( error != noErr ) goto error;
-
- // Process the item.
-
- error = ProcessItem( &fs );
- if( error != noErr ) goto error;
- }
-
- // Dispose of the list.
-
- error = AEDisposeDesc( &list );
- return( error );
-
- error:
- AEDisposeDesc( &list );
- return( error );
- }
-
- #pragma mark -
-
- /***************************************************************************************************
- ProcessItem
- ***************************************************************************************************/
-
- OSErr ProcessItem( FSSpec *inItem )
- {
- OSErr error;
- SInt16 fileRef;
- short size;
- long count;
- DialogPtr dialog;
- short result;
- Str255 titleTemp;
- MenuHandle menu;
-
- error = 0;
-
- return( error );
- }
-
- /***************************************************************************************************
- AEGetNthFSSpec
-
- Pulls out a single FSSpec from an AppleEvent document list.
- ***************************************************************************************************/
-
- OSErr AEGetNthFSSpec( AEDescList *documentList, long index, FSSpec *spec )
- {
- OSErr error;
- AEKeyword keyword;
- DescType returnedType;
- FSSpec tempSpec;
- Size actualSize;
-
- // Get the the Nth pointer in the AppleEvent of type FSSpec.
-
- error = AEGetNthPtr( documentList, index, typeFSS, &keyword, &returnedType,
- ( Ptr ) &tempSpec, sizeof( tempSpec ), &actualSize );
- *spec = tempSpec;
- return( error );
- }
-